package com.home.common.utils.query;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * Description: 分页工具类
 * Created by 王大宸 on 2020-09-28 20:18
 * Created with IntelliJ IDEA.
 */
public class QueryParams extends LinkedHashMap<String, Object> {

    private int page = 1;
    private int limit = 10;

    /***
     * Description：分页参数赋值,如果没有分页参数,默认查询前10条数据
     *
     * @author 王大宸
     * @date 2020-2-7 12:55
     * @param params page/limit
     * @return
     */
    public QueryParams(Map<String, Object> params) {
        this.putAll(params);
        if (params.get("page") != null) {
            this.page = Integer.parseInt(params.get("page").toString());
        }
        if (params.get("limit") != null) {
            this.limit = Integer.parseInt(params.get("limit").toString());
        }
        this.remove("page");
        this.remove("limit");
        this.remove("request_time");
    }

    /***
     *  不使用分页工具下, MySql 数据库分页公式转换
     *  如果没有分页参数,默认查询前10条数据
     *
     * @author 王大宸
     * @date 2020-2-7 12:55
     * @param params page/limit
     * @return void
     */
    public static void limit(Map<String, Object> params) {
        int pageSize = 1;
        int limitSize = 10;
        if (params.get("page") != null) {
            pageSize = Integer.parseInt(params.get("page").toString());
        }
        if (params.get("limit") != null) {
            limitSize = Integer.parseInt(params.get("limit").toString());
        }
        params.put("page", (pageSize - 1) * limitSize);
        params.put("limit", limitSize);
    }


    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public int getLimit() {
        return limit;
    }

    public void setLimit(int limit) {
        this.limit = limit;
    }
}
